// pages/user_wish_list/user_wish_list.js
var app = getApp();
const $ = require('../../utils/utils.js')
const config = require('../../config')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        outstockUrl: "../../img/wish_outstock.png",
        normalUrl: "../../img/wishIconBg.png",
        gridList: [],
        selBook:false,
        isLoading:true,
        footerConfig: {
            loadingText: "正在努力的加载数据...",
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getWishList()
    },

    onShow: function () {
        wx.removeStorageSync("wish_bookgrid")
    },


    didTapScanBtn: function (el) {
        console.log(el.currentTarget.dataset.grid)
        wx.setStorageSync("scanIsFromWishList", true)
        wx.setStorageSync("wish_list_grid", el.currentTarget.dataset.grid.grid_code)
        wx.setStorageSync("wish_bookgrid", el.currentTarget.dataset.grid)

        app.scanCode()
        wx.navigateBack({
        })
    },

    tapBookAction: function (el) {
        console.log(el)
        this.enterBookDetail(el.currentTarget.dataset.book)
    },

    showDeletIcon: function (el) {
        console.log(el)
        let sel = this.data.selBook

        this.setData({
            selBook: !sel
        })
    },

    deleteWishBook: function (el) {
        wx.setStorageSync('isBackFromWishList', true)
        let that = this
        let book = el.currentTarget.dataset.book;
        let url = config.removeWishBook;
        let params = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken'),
            isbn: book.isbn
        }
        console.log("#wish_list_deleteWishBook()_params", params)
        $.post(url, params).then(data => {
            console.log("#wish_list_deleteWishBook()",data)
            if (data.result == 1) {
                let wishCount = wx.getStorageSync("wishCount")
                let currentWishCount = wishCount - 1
                wx.setStorageSync("wishCount", currentWishCount)
                wx.showToast({
                    title: "已从书架移除",
                    duration: 1000
                });
                that.getWishList()
            }
        })

    },

    blockTapAction:function(){
        // 单纯为了阻碍tap事件，防止在进行longtap时，出发tap
    },
    // 进入书籍详情页
    enterBookDetail: function (book) {
        wx.setStorageSync("book_detail_isbn", book.isbn);
        let url = config.page.bookDetail + '?book_isbn=' + book.isbn + '&book_name=' + $.subChineseString(book.bookname, 20, true)
        $.goto(url)
    },

    getWishList: function () {
        var that = this;
        let params = {
            shelf_code: wx.getStorageSync("bookShelfCode"),
            userid: wx.getStorageSync("userid"),
            usertoken: wx.getStorageSync("usertoken")
        }
        let url = $.getUrl(config.queryWishList, params)
        $.get(url).then(data => {

            console.log(data)
            let gridList = Array.from(data.items).map(item => {
                if (item.grid_code === "zanque") {
                    item.grid_name = "暂缺"
                    item.type = 0
                } else {
                    item.type = 1
                    item.grid_name = item.grid_code.substr(item.grid_code.length - 1, 1) + "号柜门"
                }

                item.wishList = item.wishList.map(book => {
                    book.bookname = $.subChineseString(book.bookname, 8, true)
                    book.imgurl = book.imgurl + "?x-oss-process=image/resize,w_140"
                    return book
                })
                return item
            })

            that.setData({
                borrowList: gridList,
                isLoading:false
            })
        }).catch(error => {
            console.log("error")
            that.setData({
                isLoading: false
            })
        })
    }



})